<?PHP
    if (!defined("__PLAYPLUS__")) exit;

    class gcm {
        public function regist ($d_code, $p_code) {
            $mysql=new mysql;

            $query="INSERT INTO gcm_device (p_code, d_code, d_regdate) VALUES ('$p_code', '$d_code', UNIX_TIMESTAMP())";
            $result=$mysql->query($query);

            return $result;
        }

        public function unregist ($d_code, $p_code) {
            $mysql=new mysql;

            $query="DELETE FROM gcm_device WHERE p_code='$p_code' AND d_code='$d_code'";
            $result=$mysql->query($query);

            return $result;
        }

        public function post ($r_code=array(), $apiKey, $message) {
            $url='https://android.googleapis.com/gcm/send';
            $fields=array('registration_ids'=>$r_code, 'data'=>array("message"=>$message));
            $headers=array ('Authorization: key='.$apiKey, 'Content-Type:application/json');

            $ch=curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

            $result=curl_exec($ch);
            curl_close($ch);

            return $result;
        }
    }
?>